﻿// <copyright file="DebugMessageCollection.cs" company="TenneySoftware">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>Robert Tenney</author>
// <email>rob10e@yahoo.com</email>
// <date>April 2009</date>
// <summary>Collection of debug messages.</summary>

// License:

// Product: Engine.csproj
// Author: Robert N. Tenney
// Copyright: 9/20/2009

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation version 2
// of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// This license covers all source code, binaries, and other materials related to this project
// unless specifically stated otherwise.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace TenneySoftware.Engine.Debug {
    /// <summary>
    /// Collection of debug messages.
    /// </summary>
    public class DebugMessageCollection : Collection<DebugBuffer>, IComparer<DebugBuffer> {
        /// <summary>
        /// The term to use for searching.
        /// </summary>
        private string searchTerm;
        
        /// <summary>
        /// Initializes a new instance of the DebugMessageCollection class.
        /// </summary>
        public DebugMessageCollection() : base(new List<DebugBuffer>()) {
        }

        /// <summary>
        /// Searches the collection using the given Predicate.
        /// </summary>
        /// <param name="match">Predicate to use to search the collection.</param>
        /// <returns>True if the predicate finds a match.</returns>
        public DebugBuffer Find(Predicate<DebugBuffer> match) {
            List<DebugBuffer> items = (List<DebugBuffer>)Items;
            return items.Find(match);
        }

        /// <summary>
        /// Finds a DebugBuffer item from the given identifier.
        /// </summary>
        /// <param name="identifier">Identifier to search for.</param>
        /// <returns>The DebugBuffer item.</returns>
        public DebugBuffer Find(string identifier) {
            this.searchTerm = identifier;
            return this.Find(this.Search);
        }

        /// <summary>
        /// Checks the collection for the DebugBuffer item with the given identifier.
        /// </summary>
        /// <param name="identifier">Identifier to search for.</param>
        /// <returns>True if the item is in the collection.</returns>
        public bool Contains(string identifier) {
            List<DebugBuffer> items = (List<DebugBuffer>)Items;
            foreach (DebugBuffer item in items) {
                if (string.Compare(item.Identifier, identifier, StringComparison.OrdinalIgnoreCase) == 0) {
                    return true;
                }
            }
            
            return false;
        }

        /// <summary>
        /// Searches the collection using the given Predicate.
        /// </summary>
        /// <param name="match">Predicate to use to search the collection.</param>
        /// <returns>Index of the match.</returns>
        public int FindIndex(Predicate<DebugBuffer> match) {
            List<DebugBuffer> items = (List<DebugBuffer>)Items;
            return items.FindIndex(match);
        }

        /// <summary>
        /// Finds a DebugBuffer item's index from the given identifier.
        /// </summary>
        /// <param name="identifier">Identifier to search for.</param>
        /// <returns>The DebugBuffer item's index.</returns>
        public int FindIndex(string identifier) {
            this.searchTerm = identifier;
            return this.FindIndex(this.Search);
        }

        /// <summary>
        /// Removes an item from the collection with the given identifier.
        /// </summary>
        /// <param name="identifier">Identifier to remove.</param>
        public void Remove(string identifier) {
            int index = this.FindIndex(identifier);
            this.RemoveAt(index);
        }

        /// <summary>
        /// Sorts the collection alphabetically.
        /// </summary>
        public void Sort() {
            List<DebugBuffer> items = (List<DebugBuffer>)Items;
            items.Sort();
        }

        /// <summary>
        /// Convert this Collection into an Array.
        /// </summary>
        /// <returns>An array of DebugBuffer.</returns>
        public DebugBuffer[] ToArray() {
            List<DebugBuffer> list = (List<DebugBuffer>)this.Items;
            DebugBuffer[] arr;
            arr = list.ToArray();
            return arr;
        }

        /// <summary>
        /// Compares two DebugBuffer items using their priority.
        /// </summary>
        /// <param name="x">First item to compare.</param>
        /// <param name="y">Second item to compare.</param>
        /// <returns>Less then zero if the first item is less than the second, zero if they are the same, and greater
        /// than zero if the first is greater than the second.</returns>
        public int Compare(DebugBuffer x, DebugBuffer y) {
            return x.CompareTo(y);
        }

        /// <summary>
        /// Searches the collection for the searchTerm. Used with the Find methods.
        /// </summary>
        /// <param name="item">DebugBuffer item to compare to the searchTerm.</param>
        /// <returns>True if a match is found.</returns>
        private bool Search(DebugBuffer item) {
            return string.Compare(item.Identifier, this.searchTerm, StringComparison.OrdinalIgnoreCase) == 0;
        }
    }
}
